package com.ld.controller;

import com.ld.dto.LoginDTO;
import com.ld.service.ILoginService;
import com.ld.basic.utils.AjaxResult;
import com.ld.basic.utils.LoginMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class LoginController {

    @Autowired
    private ILoginService service;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginDTO loginDTO){

        return service.login(loginDTO);
    }


    @GetMapping("/logout")
    public AjaxResult logout(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            LoginMap.map.remove(token);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("退出失败");
        }
    }
}
